//
//  MessageCriterion-Extras.m
//  funkydog
//
//  Created by Saptarshi Guha on 1/19/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "MessageCriterion-Extras.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>
#import <objc/objc-class.h>
#import "FunkyDog.h"

@implementation MessageCriterion(Extras)
+(void)load{
	if (self == [MessageCriterion class]){
		Method originalMethod ,replacedMethod;
	//	originalMethod  = class_getClassMethod(self, @selector(stringForCriterionType:)); 
	//	replacedMethod  = class_getClassMethod(self, @selector(mystringForCriterionType:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getClassMethod(self, @selector(criterionTypeForString:)); 
		replacedMethod  = class_getClassMethod(self, @selector(mycriterionTypeForString:));
		method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(setCriterionIdentifier:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(mysetCriterionIdentifier:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(_setCriterionIdentifier:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(my_setCriterionIdentifier:));
	 	//method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(fixOnceWithExpandedSmartMailboxes:)); 
		replacedMethod  = class_getInstanceMethod(self, @selector(myfixOnceWithExpandedSmartMailboxes:));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(doesMessageSatisfyCriterion:)); 
		replacedMethod  = class_getInstanceMethod(self, @selector(mydoesMessageSatisfyCriterion:));
		method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(criterionByMergingBodyCriteria)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(mycriterionByMergingBodyCriteria));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(initWithDictionary:andRemoveRecognizedKeysIfMutable:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(myinitWithDictionary:andRemoveRecognizedKeysIfMutable:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(stringForRuleQualifier:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(mystringForRuleQualifier:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(ruleQualifierForString:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(myruleQualifierForString:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(simplifyOnce)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(mysimplifyOnce));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(containsBodyCriterion)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(mycontainsBodyCriterion));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(_evaluateBodyCriterion:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(my_evaluateBodyCriterion:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(_evaluateHeaderCriterion:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(my_evaluateHeaderCriterion:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(doesMessageSatisfyCriterion:)); 
	//	replacedMethod  = class_getInstanceMethod(self, @selector(mydoesMessageSatisfyCriterion:));
	//	method_exchangeImplementations(originalMethod, replacedMethod);
		
	}
}

//- (BOOL)mydoesMessageSatisfyCriterion:(id)fp8{
//	BOOL d=[self mydoesMessageSatisfyCriterion:fp8];
//	NSLog(@"%d=[MessageCriterion mydoesMessageSatisfyCriterion:%@",d,fp8);
//	return d;
//}
//- (BOOL)my_evaluateHeaderCriterion:(id)fp8{
//	BOOL d=[self my_evaluateHeaderCriterion:fp8];
//	NSLog(@"%d=[MessageCriterion _evaluateHeaderCriterion:%@",d,fp8);
//	return d;
//}
//-(BOOL)my_evaluateBodyCriterion:(id)fp8{
//	BOOL d=[self my_evaluateBodyCriterion:fp8];
//	NSLog(@"%d=[MessageCriterion _evaluateBodyCriterion:%@",d,fp8);
//	return d;
//}//
//-(BOOL)mycontainsBodyCriterion{
//	BOOL d=[self mycontainsBodyCriterion];
//	NSLog(@"Yes? %d",d);
//	return d;
//}//
//-(id)mysimplifyOnce{
//	
//	id v;
//	v=[self mysimplifyOnce];
//	NSLog(@"%@=[MessageCriterion simplifyOnce]",v);
//	return v;
//	
//}
//-(int)myruleQualifierForString:(id)fp8{
//	int d=[self myruleQualifierForString:fp8];
////	NSLog(@"%d=[MessageCriterion ruleQualifierForString:%@",d,fp8);
//	return d;
//}
//-(id)mystringForRuleQualifier:(int)fp8{
//	id v=[self mystringForRuleQualifier:fp8];
////	NSLog(@"%@=[MessageCriterion stringForRuleQualifier:%d",v,fp8);
//	return v;
//}
//-(id)myinitWithDictionary:(id)fp8 andRemoveRecognizedKeysIfMutable:(BOOL)fp12
//{
//	id v=[self myinitWithDictionary:fp8 andRemoveRecognizedKeysIfMutable:fp12];
////	NSLog(@"%@=[MessageCriterion myinitWithDictionary:%@ andRemoveRecognizedKeysIfMutable:%d",v,fp8,fp12);
//	return v;
//}

-(BOOL)mydoesMessageSatisfyCriterion:(id)fp8{
	BOOL d=[self mydoesMessageSatisfyCriterion:fp8];
	if( [[self criterionIdentifier] isEqualToString:TAGID] && [fp8 path]){
		BOOL notcontains=[self qualifier]==4;
		MDItemRef item = MDItemCreate(kCFAllocatorDefault, (CFStringRef)[fp8 path]);
		CFTypeRef ref = MDItemCopyAttribute(item, kMDItemKeywords);
		NSPredicate * predicate;;
		predicate = [NSPredicate predicateWithFormat:@"SELF like[cdw] %@",[self expression]];
		if ( [[(NSArray*)ref filteredArrayUsingPredicate:predicate] count]>=1 && notcontains==NO ) d=YES;
		else if ( [[(NSArray*)ref filteredArrayUsingPredicate:predicate] count]==0 && notcontains==YES ) d=YES;
		//Crashes on POP messages, since this runs on every message, pop messages dont have a path!
	}else
	if( [[self criterionIdentifier] isEqualToString:NOTEID] && [fp8 path]){
		BOOL notcontains=[self qualifier]==4;
		MDItemRef item = MDItemCreate(kCFAllocatorDefault, (CFStringRef)[fp8 path]);
		CFTypeRef ref = MDItemCopyAttribute(item, kMDItemComment);
		NSPredicate * predicate;;
		predicate = [NSPredicate predicateWithFormat:@"SELF like[cdw] %@",[self expression]];
		if ( [[(NSArray*)ref filteredArrayUsingPredicate:predicate] count]>=1 && notcontains==NO ) d=YES;
		else if ( [[(NSArray*)ref filteredArrayUsingPredicate:predicate] count]==0 && notcontains==YES ) d=YES;
			//Crashes on POP messages, since this runs on every message, pop messages dont have a path!
		}
	else
		if( [[self criterionIdentifier] isEqualToString:STEROIDID] && [fp8 path]){
			//Need to perform a MDQuery on one file! HOW?
			//Crashes on POP messages, since this runs on every message, pop messages dont have a path!
		}
	
	
	//BODYNOTE and STEROIDS
	//DOES CONTAIN// DOES NOT CONTAIN
	//see http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html
	return d;
}
- (id)myfixOnceWithExpandedSmartMailboxes:(id)fp8{

	NSString *xx=[self expression];
	int qual=[self qualifier];
	NSString *criid=[self criterionIdentifier];
	//NSLog(@"Self:%@",self);
	id v=[self myfixOnceWithExpandedSmartMailboxes:fp8];
	if([ criid isEqualToString:TAGID]) { //[FunkySob sharedInstance] istagsearch]
		//When the CriID=Body and Type=1(Entire Message) it converts 
		//Crid=MatadorBody and to Type=34, Expression=(*="string*"cdw || kMDItemtextcontent="baazi"cdw
		[v _setCriterionIdentifier:@"MatadorBody"]; 
		NSString *pref;
		if(qual==4) pref=@"(kMDItemKeywords!=\""; else pref=@"(kMDItemKeywords=\"";
		NSString *pref2=[pref stringByAppendingString:xx];
		NSString *fin=[pref2 stringByAppendingString:@"\"cdw)"];
		[v setExpression:fin];
		[v setCriterionType:34];
	}
	else if([ criid isEqualToString:NOTEID]) {//[[FunkySob sharedInstance] isnotesearch]
		//When the CriID=Body and Type=1(Entire Message) it converts 
		//Crid=MatadorBody and to Type=34, Expression=(*="string*"cdw || kMDItemtextcontent="baazi"cdw
		[v _setCriterionIdentifier:@"MatadorBody"]; 
		NSString *pref;
		if(qual==4) pref=@"(kMDItemComment!=\""; else pref=@"(kMDItemComment=\"";
		NSString *pref2=[pref stringByAppendingString:xx];
		NSString *fin=[pref2 stringByAppendingString:@"\"cdw)"];
		[v setExpression:fin];
		[v setCriterionType:34];
	}
	else if([ criid isEqualToString:STEROIDID]) {//[[FunkySob sharedInstance] isnotesearch]
		//When the CriID=Body and Type=1(Entire Message) it converts 
		//Crid=MatadorBody and to Type=34, Expression=(*="string*"cdw || kMDItemtextcontent="baazi"cdw
		[v _setCriterionIdentifier:@"MatadorBody"]; 
		NSString *pref=@"(";
		NSString *pref2=[pref stringByAppendingString:[ [FunkySob sharedInstance]  fixMDQueryString:xx]];
		NSString *fin=[pref2 stringByAppendingString:@")"];
		[v setExpression:fin];
		[v setCriterionType:34];
	//	NSLog(@"%@",v);
	}
	//NSLog(@"Self=%@, Returning %@",self,v);
	
	//NSLog(@"We have %@",v);
		return v;
}
//creates empty bodies
//-(void)mysetCriterionIdentifier:(id)fp8{
//	 [self mysetCriterionIdentifier:fp8];
////	NSLog(@"-[MessageCriterion setCriterionidentifier:%@],",fp8);
//}
//-(void)my_setCriterionIdentifier:(id)fp8{
//	
//	[self my_setCriterionIdentifier:fp8];
////	NSLog(@"-[MessageCriterion _setCriterionidentifier:%@],",fp8);
//}
//
//+(id)mystringForCriterionType:(int)fp8{
//	id v=[self mystringForCriterionType:fp8];
//	
//	//NSLog(@"%@=[MessageCriterion stringForCriterionType:%d]",v,fp8);
//	return v;
//}
+(int)mycriterionTypeForString:(id)fp8{
	int v;
	v=[self mycriterionTypeForString:fp8];
	if([fp8 isEqualToString:TAGID]) v=[self mycriterionTypeForString:@"Body"];
	else if([fp8 isEqualToString:NOTEID]) v=[self mycriterionTypeForString:@"Body"];
	else if([fp8 isEqualToString:STEROIDID]) v=[self mycriterionTypeForString:@"Body"];

	//NSLog(@"%d=[MessageCriterion criterionTypeForString:%@]",v,fp8);
	return v;
}
//
//- (id)mycriterionByMergingBodyCriteria{
//	
//	id u=[self mycriterionByMergingBodyCriteria];
////	NSLog(@"%@=[MessageCriterion mycriterionByMergingBodyCriteria]",u);
//	return u;
//}
@end

